package com.rlyy.basic.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 链路追踪工具类
 *
 * @author yuanqinglong
 * @since 2021/10/12 16:14
 */
public class TraceUtil {


    /**
     * 主机ip
     *
     * @return {@link String}
     */
    public static String hostIp(){
        InetAddress localHost = null;
        try {
            localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return stringTo16(localHost.getHostAddress());
    }

    /**
     * 字符串转化成为16进制字符串
     *
     * @param string 字符串
     * @return {@link String}
     */
    public static String stringTo16(String string) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            int ch = string.charAt(i);
            String s4 = Integer.toHexString(ch);
            str.append(s4);
        }
        return str.toString();
    }

}
